<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Transms_model extends CI_Model {
    
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
 
	function add_mo($userid,$serviceid,$commandcode,$message,$requestid){
		$this->load->database();
    	$data = array( 
            'dest'=>$serviceid,
            'phone'=>$userid,
    		'content'=>$message,
    		'createdate'=>date("d-m-Y h:i:s"),
    		'cmdcode'=>$commandcode,
    		'requestId' =>$requestid
            );
    	$this->db->insert('tbmos', $data); 
	}
	
	function show_mo($num,$offset){ 
		$this->load->database(); 
		$this->db->where('today',date('d-m-Y'));
		$this->db->order_by("id", "desc"); 
		$query=$this->db->get('tbmos',$num,$offset);
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}
	
	function show_mo_sum($num,$offset){ 
		$this->load->database();  
		$this->db->order_by("id", "desc"); 
		$query=$this->db->get('tbmos',$num,$offset);
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}
	
	function totalmotoday($datetotal=null){  
    	$this->load->database(); 
    		$query =  $this->db->query("select count(*) as total from tbmos where today='".$datetotal."'"); 
    		if ($query->num_rows() > 0) {
    			foreach($query->result() as $row){
    				return $row->total;
    			}
    		}else{
    			return 0;
    		} 
	}
	
	function totalmobymonth($thang=null,$nam=null){
		$this->load->database(); 
		if($thang==null || $nam==null){
			$query =  $this->db->query("select count(*) as total from tbmos");
		}else{
			$query =  $this->db->query("select count(*) as total from tbmos where  today like '%".$thang."-".$nam."'");
		} 
		if ($query->num_rows() > 0) {
			foreach($query->result() as $row){
				return $row->total;
			}
		}else{
			return 0;
		}
	}
	
	
	function totalmo($id=null,$type=null,$datetotal=null){  
    	$this->load->database();
    	if($id==null && $type==null){
			return $this->db->count_all('tbmos');
    	}else{ 
    		//$this->db->where('phone',$id);
    		if($datetotal <> null){
    		$query =  $this->db->query("select count(*) as total from tbmos where today='".$datetotal."'");
    		}else{
    		$query =  $this->db->query("select count(*) as total from tbmos where ".$type."=".$id);	
    		}
    		if ($query->num_rows() > 0) {
    			foreach($query->result() as $row){
    				return $row->total;
    			}
    		}else{
    			return 0;
    		}
    		 
    	} 
	}
	
	function show_mobytype($where,$value,$num,$offset){ 
		$this->load->database();
		$this->db->where($where,$value);
		$this->db->order_by("id", "desc");  
		$query=$this->db->get('tbmos',$num,$offset);
		if ($query->num_rows() > 0)
		{
			return $query->result();
		}
		return $query->result();
	}
	
	 
	
	
}
    ?>